package com.zyq.service.impl;

import java.util.*;

import com.zyq.model.entity.Field;
import com.zyq.model.entity.HealthAssessment;
import com.zyq.model.response.FieldResponse;
import com.zyq.model.response.OptionResponse;
import com.zyq.model.response.TableResponse;
import org.apache.commons.collections.map.MultiValueMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zyq.mapper.HealthAssessmentMapper;
import com.zyq.service.IHealthAssessmentService;

/**
 * 健康评估表Service业务层处理
 *
 * @author zyq
 * @date 2021-12-08
 */
@Service
public class HealthAssessmentServiceImpl implements IHealthAssessmentService
{
    @Autowired
    private HealthAssessmentMapper healthAssessmentMapper;

    /**
     * 查询健康评估表
     *
     * @param id 健康评估表ID
     * @return 健康评估表
     */
    @Override
    public HealthAssessment selectHealthAssessmentById(Long id)
    {
        return healthAssessmentMapper.selectHealthAssessmentById(id);
    }

    /**
     * 查询字段
     */
    @Override
    public List<Field> selectFieldList(){
        return healthAssessmentMapper.selectFieldList();
    }

    /**
     * 查询健康评估表列表
     *
     * @return 健康评估表
     */
    @Override
    public Map<String, Map<String,Long>> selectHealthAssessmentMap()
    {
        Map<String, Map<String,Long>> fields = new HashMap<>();
        List<FieldResponse> fieldResponseList = healthAssessmentMapper.selectListAll();
        for(FieldResponse fieldResponseTemp : fieldResponseList){
            Map<String,Long> map = fields.getOrDefault(fieldResponseTemp.getFieldName(),new HashMap<String,Long>());
            map.put(fieldResponseTemp.getOption(),fieldResponseTemp.getScore());
            fields.put(fieldResponseTemp.getFieldName(),map);
          }
        return fields;
//        List<TableResponse> tableResponseList = new ArrayList<>();
//        for(int i = 0 ; i < fieldResponseList.size(); i++){
//            List<OptionResponse> optionResponseList = new ArrayList<>();
//            while(i < fieldResponseList.size()-1 && fieldResponseList.get(i).getFieldName().equals(fieldResponseList.get(i+1).getFieldName())){
//                OptionResponse optionResponse = new OptionResponse()
//                        .setOption(fieldResponseList.get(i).getOption())
//                        .setScore(fieldResponseList.get(i).getScore());
//                optionResponseList.add(optionResponse);
//                i++;
//            }
//            TableResponse tableResponse = new TableResponse()
//                    .setFieldName(fieldResponseList.get(i).getFieldName())
//                    .setOptions(optionResponseList);
//            tableResponseList.add(tableResponse);
//        }
//        return tableResponseList;
    }

    @Override
    public List<HealthAssessment> selectHealthAssessmentList(Long fieldId)
    {
        return healthAssessmentMapper.selectHealthAssessmentList( fieldId);

    }


    /**
     * 新增健康评估表
     *
     * @param healthAssessment 健康评估表
     * @return 结果
     */
    @Override
    public int insertHealthAssessment(HealthAssessment healthAssessment)
    {
        return healthAssessmentMapper.insertHealthAssessment(healthAssessment);
    }

    /**
     * 修改健康评估表
     *
     * @param healthAssessment 健康评估表
     * @return 结果
     */
    @Override
    public int updateHealthAssessment(HealthAssessment healthAssessment)
    {
        return healthAssessmentMapper.updateHealthAssessment(healthAssessment);
    }

    /**
     * 批量删除健康评估表
     *
     * @param ids 需要删除的健康评估表ID
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentByIds(Long[] ids)
    {
        return healthAssessmentMapper.deleteHealthAssessmentByIds(ids);
    }

    /**
     * 删除健康评估表信息
     *
     * @param id 健康评估表ID
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentById(Long id)
    {
        return healthAssessmentMapper.deleteHealthAssessmentById(id);
    }

    @Override
    public int  updateField(Field field){
        return healthAssessmentMapper.updateField(field);
    }

    @Override
    public int insertField(Field field){
        return healthAssessmentMapper.insertField(field);
    }

    @Override
    public int  removeField(Long[] ids){
        return healthAssessmentMapper.removeField(ids);
    }
}
